#pragma once 
#include<iostream>
#include<sys/epoll.h>
#include<unistd.h>
#include"Log.hpp"
#include"Comm.hpp"

static const int gsize = 128;

class Epoller
{
    private:
    bool EventMethodCore(int fd,uint32_t events,int type)
    {
        struct epoll_event ev;
        ev.events = events;
        ev.data.fd = fd;
        int n = ::epoll_ctl(_epfd,type,fd,&ev);//注册
        if(n<0)
        {
        LOG(ERROR,"epoll_ctl error\n");
            return false;
        }
        //日志
        LOG(DEBUG,"epoll_ctl add %d success\n\n",fd);
        return true;
    }
    public:
    Epoller()
    {
        _epfd = ::epoll_create(gsize);//通过epoll文件描述符标识内核中的事件表能够使的它在单个到左中同时监听多个io
        if(_epfd<0)
        {
            LOG(FATAL,"epollcreate error\n");
            exit (EPOLL_CREATE_ERROR);
        }
        LOG(DEBUG,"epoll create success,epfd %d\n",_epfd);
    }
    bool AddEven(int fd,uint32_t events)
    {
        return EventMethodCore(fd,events,EPOLL_CTL_ADD);//添加；
    }
    bool ModEvent(int fd,uint32_t events)
    {
        return EventMethodCore(fd,events,EPOLL_CTL_MOD);//修改
    }
    bool DelEvent(int fd)
    {
        return ::epoll_ctl(_epfd,EPOLL_CTL_DEL,fd,nullptr);//删除
    }
    int Wait(struct epoll_event revs[],int num,int timeout)
    {
        int n = ::epoll_wait(_epfd,revs,num,timeout);
        return n;
    }
    ~Epoller()
    {
        if(_epfd>=0)
        {
            ::close(_epfd);
        }
    }
    private:
    int _epfd;
};